package com.zjdiepu.www.biz.yw;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.ModeIDType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.his.HisYwBusinessBuyerMapper;
import com.zjdiepu.www.dao.yw.YwBusinessBuyerMapper;
import com.zjdiepu.www.ibiz.yw.IYwBusinessBuyerService;
import com.zjdiepu.www.ibiz.zpp.SystemService;
import com.zjdiepu.www.model.his.dto.HisYwBusinessBuyer;
import com.zjdiepu.www.model.yw.dto.YwBusinessBuyer;
import com.zjdiepu.www.model.yw.ext.YwBusinessBuyerExt;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 购车人信息Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年07月28日 15时04分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwBusinessBuyerServiceImpl extends BaseService implements IYwBusinessBuyerService {
	public static final Logger logger = LoggerFactory.getLogger(YwBusinessBuyerServiceImpl.class);

	@Autowired
	private YwBusinessBuyerMapper ywBusinessBuyerMapper;
	@Autowired
	private HisYwBusinessBuyerMapper hisYwBusinessBuyerMapper;
	@Autowired
	private SystemService systemService;
	
	@Override
	public void saveUpdateAndDelete(YwBusinessBuyer ywBusinessBuyer, OperateType operType) {
		ywBusinessBuyer.setIdno(ywBusinessBuyer.getIdno() == null ? null : ywBusinessBuyer.getIdno().toUpperCase());
		switch (operType) {
		case add:
			ywBusinessBuyer.initInsertData(ywBusinessBuyer.getModifier(), ywBusinessBuyer.getModifierName());
			ywBusinessBuyerMapper.insert(ywBusinessBuyer);
			break;
		case edit:
			//保存历史购车人信息
			if(ywBusinessBuyer.getSaveHis() != null && ywBusinessBuyer.getSaveHis()) {
				String modifier = ywBusinessBuyer.getModifier();
				String modifierName = ywBusinessBuyer.getModifierName();
				String buyerId = ywBusinessBuyer.getId();
				YwBusinessBuyer dbYwBusinessBuyer = ywBusinessBuyerMapper.selectByPrimaryKey(buyerId);
				if(!dbYwBusinessBuyer.isCompleted()) {
					ExceptionHelper.thow("9999","购车人【" + dbYwBusinessBuyer.getName() + "】信息不完整");
				}
				HisYwBusinessBuyer hisYwBusinessBuyer = new HisYwBusinessBuyer();
				String busiId = dbYwBusinessBuyer.getBusiId();
				BeanUtils.copyProperties(dbYwBusinessBuyer, hisYwBusinessBuyer);
				int version = hisYwBusinessBuyerMapper.getVersion(busiId, buyerId);
				hisYwBusinessBuyer.initInsertData(modifier, modifierName);
				hisYwBusinessBuyer.setBuyerId(buyerId);
				hisYwBusinessBuyer.setVersionNo(version + 1);
				hisYwBusinessBuyerMapper.insert(hisYwBusinessBuyer);
				
				systemService.diepuNewsHandle(new AbstractSysUser() {
					
					@Override
					public String getName() {
						return ywBusinessBuyer.getModifierName();
					}
					
					@Override
					public String getId() {
						return ywBusinessBuyer.getModifier();
					}
				}, ModeIDType.XGGCR.getCode(), "31", busiId, null);
			}
			ywBusinessBuyer.initUpdateData(ywBusinessBuyer.getModifier(), ywBusinessBuyer.getModifierName());
			// 影响的记录数
			int updateCount = ywBusinessBuyerMapper.updateByPrimaryKeySelective(ywBusinessBuyer);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			
			break;
		case delete:
			ywBusinessBuyer.initDeleteData(ywBusinessBuyer.getModifier(), ywBusinessBuyer.getModifierName());
			// 影响的记录数
			int deleteCount = ywBusinessBuyerMapper.updateByPrimaryKeySelective(ywBusinessBuyer);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwBusinessBuyer> selectByExt(YwBusinessBuyerExt ywBusinessBuyerExt) {
		return ywBusinessBuyerMapper.selectByExt(ywBusinessBuyerExt);
	}
	
	@Override
	public int countByExt(YwBusinessBuyerExt ywBusinessBuyerExt){
		return ywBusinessBuyerMapper.countByExt(ywBusinessBuyerExt);
	}
	
	@Override
	public YwBusinessBuyer selectById(String id) {
		return ywBusinessBuyerMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywBusinessBuyerMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwBusinessBuyer ywBusinessBuyer) {
		return ywBusinessBuyerMapper.countRefined(ywBusinessBuyer);
	}

	@Override
	public YwBusinessBuyer selectRefined(YwBusinessBuyer ywBusinessBuyer) {
		return ywBusinessBuyerMapper.selectRefined(ywBusinessBuyer);
	}
	
	@Override
	public int deletedRefined(YwBusinessBuyer ywBusinessBuyer) {
		return ywBusinessBuyerMapper.deletedRefined(ywBusinessBuyer);
	}

	@Override
	public boolean repeat(YwBusinessBuyer ywBusinessBuyer) {
		int count = countRefined(ywBusinessBuyer);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywBusinessBuyer.getId())) {
			return true;
		}
		YwBusinessBuyer dbItem = selectRefined(ywBusinessBuyer);
		if(null == dbItem) {
			return false;
		}
		return !ywBusinessBuyer.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywBusinessBuyerMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<YwBusinessBuyer> ywBusinessBuyers) {
		return ywBusinessBuyerMapper.insertBatch(ywBusinessBuyers);
	}
}
